const mongoUtil = require("./mongo_util")
const collectionName = "students"

function register(value, callback) {
    console.log("value", value);
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({
            'SecondaryCollege': value.SecondaryCollege
        }).toArray(function (err, result) {
            if (result.length > 0) {
                // console.log("result",result);
                const classList=result[0].classList.filter(i => {
                    return i.className === value.className
                })
                // classList.forEach(item => {
                    if (classList.length>0) {
                        // console.log("111111");
                        const studentList=classList[0].studentList.filter(ite=>{
                            return ite.student_Number === value.student_Number
                        })
                        if (studentList.length>0) {
                            callback({
                                reason: "用户已存在"
                            }, null)
                        } else {
                            classList[0].studentList.unshift({
                                studentName: value.studentName,
                                student_Number: value.student_Number,
                                password: value.password,
                                grade: value.grade
                            })
                           
                            const whereStr = {
                                // 'SecondaryStudentList.scoreList.student_number': score.student_number,
                                'SecondaryCollege': value.SecondaryCollege,
                                // "SecondaryStudent.className": {
                                //     $ne: score.className
                                // },
                                // "SecondaryStudent.studentList.student_Number": {
                                //     $ne: score.student_number
                                // }
                            }
                            const updateStr = {
                                $set: {
                                    'classList': [...result[0].classList],
                                }
                            };
                            mongoUtil.getCollection(collectionName).then(collection => {
                                collection.updateOne(whereStr, updateStr, function (err, res) {

                                    callback(err, null)
                                });
                            })
                        }
                        // item.studentList.forEach(ite => {
                            
                        // })
                    } else {
                        result[0].classList.unshift({
                            className: value.className,
                            studentList: [{
                                studentName: value.studentName,
                                student_Number: value.student_Number,
                                password: value.password,
                                grade: value.grade
                            }]
                        })
                        const whereStr = {
                            // 'SecondaryStudentList.scoreList.student_number': score.student_number,
                            'SecondaryCollege': value.SecondaryCollege,
                            // "SecondaryStudent.className": {
                            //     $ne: score.className
                            // },
                            // "SecondaryStudent.studentList.student_Number": {
                            //     $ne: score.student_number
                            // }
                        }
                        const updateStr = {
                            $set: {
                                'classList': [...classList,...result[0].classList],
                            }
                        };
                        mongoUtil.getCollection(collectionName).then(collection => {
                            collection.updateOne(whereStr, updateStr, function (err, res) {

                                callback(err, null)
                            });
                        })
                    }
                // })
                // callback({
                //     msg: "已存在"
                // }, result)
                
                return
            }
            mongoUtil.getCollection(collectionName).then(collection => {
                collection.insertOne({
                    SecondaryCollege: value.SecondaryCollege,
                    classList: [{
                        className: value.className,
                        studentList: [{
                            studentName: value.studentName,
                            student_Number: value.student_Number,
                            password: value.password,
                            grade: value.grade
                        }]
                    }]
                }, function (err, res) {
                    callback(err, res)
                });
            })
        })
    })
}
module.exports = {
    register
}